home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / apache_1.0.5 / src / mod_log_agent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  5.7 KB  |  189 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55.  
  56. #include "httpd.h"
  57. #include "http_config.h"
  58.  
  59. module agent_log_module;
  60.  
  61. static int xfer_flags = ( O_WRONLY | O_APPEND | O_CREAT );
  62. static mode_t xfer_mode = ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH );
  63.  
  64. typedef struct {
  65.     char *fname;
  66.     int agent_fd;
  67. } agent_log_state;
  68.  
  69. void *make_agent_log_state (pool *p, server_rec *s)
  70. {
  71.     agent_log_state *cls =
  72.       (agent_log_state *)palloc (p, sizeof (agent_log_state));
  73.  
  74.     cls->fname = "";
  75.     cls->agent_fd = -1;
  76.  
  77.  
  78.     return (void *)cls;
  79. }
  80.  
  81. char *set_agent_log (cmd_parms *parms, void *dummy, char *arg)
  82. {
  83.     agent_log_state *cls = get_module_config (parms->server->module_config,
  84.                            &agent_log_module);
  85.   
  86.     cls->fname = arg;
  87.     return NULL;
  88. }
  89.  
  90. command_rec agent_log_cmds[] = {
  91. { "AgentLog", set_agent_log, NULL, RSRC_CONF, TAKE1,
  92.     "the filename of the agent log" },
  93. { NULL }
  94. };
  95.  
  96. void agent_log_child (void *cmd)
  97. {
  98.     /* Child process code for 'AgentLog "|..."';
  99.      * may want a common framework for this, since I expect it will
  100.      * be common for other foo-loggers to want this sort of thing...
  101.      */
  102.     
  103.     cleanup_for_exec();
  104.     signal (SIGHUP, SIG_IGN);
  105.     execl (SHELL_PATH, SHELL_PATH, "-c", (char *)cmd, NULL);
  106.     exit (1);
  107. }
  108.  
  109. void open_agent_log (server_rec *s, pool *p)
  110. {
  111.     agent_log_state *cls = get_module_config (s->module_config,
  112.                            &agent_log_module);
  113.   
  114.     char *fname = server_root_relative (p, cls->fname);
  115.     
  116.     if (cls->agent_fd > 0) return; /* virtual log shared w/main server */
  117.     
  118.     if (*cls->fname == '|') {
  119.     FILE *dummy;
  120.     
  121.     spawn_child(p, agent_log_child, (void *)(cls->fname+1),
  122.             kill_after_timeout, &dummy, NULL);
  123.  
  124.     if (dummy == NULL) {
  125.         fprintf (stderr, "Couldn't fork child for AgentLog process\n");
  126.         exit (1);
  127.     }
  128.  
  129.     cls->agent_fd = fileno (dummy);
  130.     }
  131.     else if(*cls->fname != '\0') {
  132.       if((cls->agent_fd = popenf(p, fname, xfer_flags, xfer_mode)) < 0) {
  133.         fprintf(stderr,"httpd: could not open agent log file %s.\n", fname);
  134.         perror("open");
  135.         exit(1);
  136.       }
  137.     }
  138. }
  139.  
  140. void init_agent_log (server_rec *s, pool *p)
  141. {
  142.     for (; s; s = s->next) open_agent_log (s, p);
  143. }
  144.  
  145. int agent_log_transaction(request_rec *orig)
  146. {
  147.     agent_log_state *cls = get_module_config (orig->server->module_config,
  148.                            &agent_log_module);
  149.   
  150.     char str[HUGE_STRING_LEN];
  151.     char *agent;
  152.     request_rec *r;
  153.  
  154.     if(cls->agent_fd <0)
  155.       return OK;
  156.  
  157.     for (r = orig; r->next; r = r->next)
  158.         continue;
  159.     if (*cls->fname == '\0')    /* Don't log agent */
  160.     return DECLINED;
  161.  
  162.     agent = table_get(orig->headers_in, "User-Agent");
  163.     if(agent != NULL) 
  164.       {
  165.     sprintf(str, "%s\n", agent);
  166.     write(cls->agent_fd, str, strlen(str));
  167.       }
  168.     
  169.     return OK;
  170. }
  171.  
  172. module agent_log_module = {
  173.    STANDARD_MODULE_STUFF,
  174.    init_agent_log,        /* initializer */
  175.    NULL,            /* create per-dir config */
  176.    NULL,            /* merge per-dir config */
  177.    make_agent_log_state,    /* server config */
  178.    NULL,            /* merge server config */
  179.    agent_log_cmds,        /* command table */
  180.    NULL,            /* handlers */
  181.    NULL,            /* filename translation */
  182.    NULL,            /* check_user_id */
  183.    NULL,            /* check auth */
  184.    NULL,            /* check access */
  185.    NULL,            /* type_checker */
  186.    NULL,            /* fixups */
  187.    agent_log_transaction    /* logger */
  188. };
  189.